<template>
  <div class="page">
    <div class="title energized">搜知涯 知生涯</div>
    <div>
      <search @search="search" :searchVal="searchVal" ref="search"></search>
      <div class="history">
        <div v-if="history">
          <a v-for="(item,index) in history" :key="index" @click="search(item)" v-if="item.length>0">{{item}}</a>
        </div>
      </div>
    </div>
    <tab-bar :tabsData="tabsData" :currentIndex="currentIndex" @tabClick="tabClick" v-if="this.tabsData.length"></tab-bar>
    <div class="tab-content scroll-wrap" v-if="isResult">
      <!-- 大学 -->
      <div v-show="currentIndex==0">
        <div class="list" v-if="college.content.length>0">
          <nuxt-link class="item item-stripe item-card item-icon-right item-avatar-left mb-10" :to="'school-detail?id='+item.id+'&fromPage=search'" v-for="(item,index) in college.content" :key="index">
            <img :src="item.schoolBadge | imgResize(50,50)" />
            <div class="item-content">
              <h3>{{item.name}}</h3>
              <p class="stable">
                <span v-if="item.levelNames" v-for="(list,i) in item.levelNames" :key="i">{{list}}</span>
                <span>{{item.typeName}}</span>
              </p>
            </div>
            <div class="button energized border-energized button-clear">
              查看详情
            </div>
          </nuxt-link>
          <paginate :total="college.totalElements" :current-page="college.page+1" @pagechange="pagechange('college',$event)"></paginate>
        </div>
        <no-content v-else>暂无数据</no-content>
      </div>
      <!-- 专业 -->
      <div v-show="currentIndex==1">
        <div class="list" v-if="major.content.length>0">
          <nuxt-link class="item item-stripe item-card item-icon-right" :to="'major-detail?id='+item.id+'&pageType=show&fromPage=search'" v-for="(item,index) in major.content" :key="index">
            <div class="item-content">
              <h3>{{item.name}}</h3>
              <p class="stable">
                <span v-if="item.schoolingTime">学制{{item.schoolingTime}}年</span>
                <span>开设院校{{item.collegeCount}}所</span>
              </p>
            </div>
            <div class="button energized border-energized button-clear">
              查看详情
            </div>
          </nuxt-link>
          <paginate :total="major.totalElements" :current-page="major.page+1" @pagechange="pagechange('major',$event)"></paginate>
        </div>
        <no-content v-else>暂无数据</no-content>
      </div>
      <!-- 职业 -->
      <div v-show="currentIndex==2">
        <div class="list" v-if="career.content.length>0">
          <nuxt-link class="item item-stripe item-card item-icon-right" :to="'profession-details?id='+item.id+'&fromPage=search'" v-for="(item,index) in career.content" :key="index">
            <div class="item-content">
              <h3>{{item.name}}</h3>
            </div>
            <div class="button energized border-energized button-clear">
              查看详情
            </div>
          </nuxt-link>
          <paginate :total="career.totalElements" :current-page="career.page+1" @pagechange="pagechange('career',$event)"></paginate>
        </div>
        <no-content v-else>暂无数据</no-content>
      </div>
    </div>
  </div>
</template>
<script>
import { api } from '@/utils'
export default {
  layout: 'xuanke',
  data () {
    return {
      searchVal: '',
      history: [],
      tabsData: [],
      isResult: false,
      currentIndex: 0,
      college: {
        content: [],
        totalElements: 0,
        page: 0
      },
      major: {
        content: [],
        totalElements: 0,
        page: 0
      },
      career: {
        content: [],
        totalElements: 0,
        page: 0
      },
      size: 15,
      count: []
    }
  },
  methods: {
    tabClick (index) {
      this.currentIndex = index
      this.college.content = []
      this.major.content = []
      this.career.content = []
      // parseInt(this.$route.query.fromWhere) !== index &&
      if (this.count[index] > 0) {
        this.getData(index)
      }
    },
    search (val) {
      this.searchVal = val
      this.$refs.search._data.searchVal = this.searchVal
      this.getSearchCount()
    },
    getSearchCount () {
      this.currentIndex = 0
      if (this.searchVal.trim().length > 0) {
        this.$axios.get(api.searchCount, { params: { keyword: this.searchVal.trim() } }).then(data => {
          this.count = []
          for (let x in data) {
            this.count.push(data[x])
          }
          this.tabsData = [`大学(${data.collegeCount})`, `专业(${data.majorCount})`, `职业(${data.careerCount})`]
          this.isResult = true
          this.college.page = 0
          this.major.page = 0
          this.career.page = 0
          this.getData(0)
          this.getHistory()
        })
      }
    },
    getData (index, pageNum) {
      let link
      let obj
      switch (index) {
        case 0:
          link = api.searchCollege
          obj = this.college
          break
        case 1:
          link = api.searchMajor
          obj = this.major
          break
        case 2:
          link = api.searchCareer
          obj = this.career
          break
      }
      if (pageNum != null) {
        obj.page = pageNum - 1
      }
      this.$axios.get(link, { params: { keyword: this.searchVal.trim(), page: obj.page, size: this.size } }).then(data => {
        if (data.content.length > 0) {
          obj.content = data.content
          obj.totalElements = data.totalElements
        } else {
          obj.content = []
          obj.totalElements = 0
        }
      })
    },
    // 分页
    pagechange (type, currentPage) {
      switch (type) {
        case 'college':
          this.college.page = currentPage - 1
          break
        case 'major':
          this.major.page = currentPage - 1
          break
        case 'career':
          this.career.page = currentPage - 1
          break
      }
      this.getData(this.currentIndex, currentPage)
    },
    // 获取历史搜索记录
    getHistory () {
      this.$axios.get(api.history).then(data => {
        this.history = data
      })
    }
  },
  beforeMount () {
    this.getHistory()
    this.$store.commit('SET_CHOOSE_SUBJECT', '')
  }
}
</script>
<style lang="scss" scoped>
.page {
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
  .title {
    font-size: 30px;
    text-align: center;
  }
}
.history {
  width: 70%;
  margin: 20px auto;
  padding-left: 50px;
  a {
    font-size: $size16;
    text-decoration: underline;
    &:not(:last-child) {
      margin-right: 20px;
    }
  }
}
.tab-content {
  flex: 1;
  // overflow-y: auto;
  // margin-top: 30px;
  padding: 0 25px;
  .list {
    .item {
      &-stripe {
        margin: 0 -20px;
      }
      height: 82px;
    }
    .item-card {
      img {
        border-radius: 50%;
        background-color: $stable;
      }
      .item-content {
        span {
          margin-right: 8px;
        }
      }
    }
  }
}
</style>
